package org.standard.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import org.standard.base.model.BaseRole;
import org.standard.base.model.BaseUser;
import org.standard.commons.exception.InfrastructureException;
import org.standard.commons.utils.DAO;
import org.standard.sys.model.SysFunc;
import org.standard.sys.model.SysMenu;
import org.standard.sys.service.SysMenuService;

public class SysMenuServiceImpl implements SysMenuService {
	private DAO dao;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';//主菜单分类代码重复的提示返回
	private static final char FAIL_2 = '3';//主菜单分类名字重复的提示返回
	private static final char FAIL_3 = '4';//主菜单分类名字重复的提示返回
	
	public DAO getDao() {
		return dao;
	}
	public void setDao(DAO dao) {
		this.dao = dao;
	}


	@Override
	public char createSysMenu(SysMenu sysMenu)throws Exception {
		// TODO Auto-generated method stub
		//主菜单分类代码是否重复
		char forward = 0;
		String hql1 = "from SysMenu where menuCode = '"+sysMenu.getMenuCode()+"'";
		List<SysMenu> list1 = dao.find(hql1);
		
		//主菜单名字是否重复
		String hql2 = "from SysMenu where menuName = '"+sysMenu.getMenuName()+"'";
		List<SysMenu>  list2 = dao.find(hql2);
		
		//主菜单英文名字是否重复
		String hql3 = "from SysMenu where menuNameEn = '"+sysMenu.getMenuNameEn()+"'";
		List<SysMenu>  list3 = dao.find(hql3);
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0){
			
			dao.save(sysMenu);
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}

	
	@Override
	public char deleteSysMenu(SysMenu sysMenu)throws Exception {
		// TODO Auto-generated method stub
		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"%'";
	    List<SysFunc> list = dao.find(hql);
	    if(list.size() == 0){
	    	dao.delete(sysMenu);
	    	return SUCCESS;
	    }else{
	    	
	    	return FAIL;
	    }
	}

//	@Override
//	public List<SysFunc> juniorSysMenuList(String menuId) {
//		// TODO Auto-generated method stub
//		SysMenu sysMenu = (SysMenu) dao.find(SysMenu.class, menuId);
//		
//		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"%' order by menuCodeGroup";
//		List<SysFunc> sysFuncs = new ArrayList<SysFunc>();
//		List<SysFunc> list = dao.find(hql);
//		for(SysFunc sysFunc:list){
//			sysFuncs.add(sysFunc);
//			sysFuncs.addAll(childSysFuncS(sysFunc.getMenuCodeGroup()));
//		}
//		return sysFuncs;
//	}
//
//	private List<SysFunc> childSysFuncS(String code){
//		List<SysFunc> sysFuncs = new ArrayList<SysFunc>();
//		String hql = "from SysFunc where menuCodeGroup like '"+code+"%' order by menuCodeGroup";
//		List<SysFunc> list = dao.find(hql);
//		for(SysFunc sysFunc:list){
//			sysFuncs.add(sysFunc);
//			List<SysFunc> childList = childSysFuncS(sysFunc.getMenuCodeGroup());
//			sysFuncs.addAll(childList);
//		}
//		return sysFuncs;
//	}
//	
//	
//	@Override
//	public List<SysMenu> juniorSysMenuList() {
//		
//		List<SysMenu>  sysMenus = new ArrayList<SysMenu>();
//		sysMenus = sysMenuList();
//		
//		if(sysMenus.size() > 0){
//			for(SysMenu sysMenu:sysMenus){
//				sysMenu.setSysFuncs(juniorSysMenuList(sysMenu.getMenuId()));
//			}
//		}
//		return sysMenus;
//	}
	

	public List<SysFunc> juniorSysMenuList2(String menuCode) {
		// TODO Auto-generated method stub
		
		String hql = "from SysFunc where menuCodeGroup like '"+menuCode+"__' order by menuCodeGroup";
		List<SysFunc> list = dao.find(hql);
		for(SysFunc sysFunc:list){
			sysFunc.setSysFuncs(dao.find("from SysFunc where menuCodeGroup like '"+sysFunc.getMenuCodeGroup()+"%'and menuCodeGroup != '"+sysFunc.getMenuCodeGroup()+"'  order by menuCodeGroup"));
		}
		return list;
	}
	

	@Override
	public List<SysMenu> juniorSysMenuList() {
		
		List<SysMenu>  sysMenus = new ArrayList<SysMenu>();
		sysMenus = sysMenuList();
		
		if(sysMenus.size() > 0){
			for(SysMenu sysMenu:sysMenus){
				sysMenu.setSysFuncs(juniorSysMenuList(sysMenu.getMenuId()));
			}
		}
		return sysMenus;
	}
	
	private List<SysFunc> childSysFuncS(String code){
//		List<SysFunc> sysFuncs = new ArrayList<SysFunc>();
		String hql = "from SysFunc where menuCodeGroup like '"+code+"%' order by menuCodeGroup";
		List<SysFunc> list = dao.find(hql);
//		for(SysFunc sysFunc:list){
//			sysFuncs.add(sysFunc);
//			List<SysFunc> childList = childSysFuncS(sysFunc.getMenuCodeGroup());
//		}
		return list;
	}
	
	
	
	
	@Override
	public char modifySysMenu(SysMenu sysMenu)throws Exception {
		// TODO Auto-generated method stub
		char forward = 0;
		String hql1 = "from SysMenu where menuCode = '"+sysMenu.getMenuCode()+"' and menuId != '"+sysMenu.getMenuId()+"'";
		List<SysMenu> list1 = dao.find(hql1);
		
		//主菜单名字是否重复
		String hql2 = "from SysMenu where menuName = '"+sysMenu.getMenuName()+"' and menuId != '"+sysMenu.getMenuId()+"'";
		List<SysMenu>  list2 = dao.find(hql2);
		
		//主菜单名字是否重复
		String hql3 = "from SysMenu where menuNameEn = '"+sysMenu.getMenuNameEn()+"' and menuId != '"+sysMenu.getMenuId()+"'";
		List<SysMenu>  list3 = dao.find(hql3);
		
		
//		String menuCode = sysMenu.getMenuCode();
		
		
		if(list1.size() == 0 && list2.size() == 0&& list3.size() == 0){
			
			dao.update(sysMenu);
//			if(StringUtils.isNotBlank(menuCode)){
//				
//				
//				modifyJuniorFunc(menuCode);
//			}
			
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}else if(list3.size() > 0){
				forward = FAIL_3;
			}
		}
		return forward;
	}

	public char modifyJuniorFunc(SysMenu sysMenu,String menuCode){
		
		
		List<SysFunc> sysfuncs =  new ArrayList(juniorSysMenuList2(menuCode));
		
		for(SysFunc sysFunc:sysfuncs){
			
			String menuCodeGroup = sysFunc.getMenuCodeGroup();
			
			String menuCodeGroup1 =  sysMenu.getMenuCode()+menuCodeGroup.substring(2, menuCodeGroup.length());
			
			sysFunc.setMenuCodeGroup(menuCodeGroup1);
			
			dao.update(sysFunc);
			
		}
	  
		return SUCCESS;
	}
	
	
	
	@Override
	public List<SysMenu> sysMenuList() {
		
		List<SysMenu>  sysMenus = new ArrayList<SysMenu>();
		List<SysMenu> list = dao.find("from SysMenu where 1=1 order by menuCode");
		
		for(SysMenu sysMenu:list){
			
			List<SysFunc> funcs = juniorSysMenuList(sysMenu.getMenuId());
			
			sysMenu.setSysFuncs(funcs);
			
			sysMenus.add(sysMenu);
		}
		
		return sysMenus;
	}
	
	
	@Override
	public List<SysFunc> juniorSysMenuList(String menuId) {
		// TODO Auto-generated method stub
		SysMenu sysMenu1 = (SysMenu) dao.find(SysMenu.class, menuId);
		
		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu1.getMenuCode()+"__' order by menuCodeGroup";
		List<SysFunc> list = dao.find(hql);
		for(SysFunc sysFunc:list){
			sysFunc.setSysFuncs(dao.find("from SysFunc where menuCodeGroup like '"+sysFunc.getMenuCodeGroup()+"%'and menuCodeGroup != '"+sysFunc.getMenuCodeGroup()+"'  order by menuCodeGroup"));
		}
		return list;
	}
	
	@Override
	public SysMenu findSysMenuById(String menuId) {
		SysMenu sysMenu = new SysMenu();
		
		sysMenu = (SysMenu)dao.find(SysMenu.class, menuId);
		
		return sysMenu;
	}
	
	
	
	
	public List<SysMenu> indexLeft(BaseUser user) {
		// TODO Auto-generated method stub
		List<SysMenu>  sysMenus1 = new ArrayList<SysMenu>();
		List<SysMenu>  sysMenus = new ArrayList<SysMenu>();
		sysMenus = dao.find("from SysMenu where 1=1 order by menuCode");
		
		String funcIdGroup = getPermissions(user);
		
			if(funcIdGroup !=null && StringUtils.isNotBlank(funcIdGroup)){
				funcIdGroup = funcIdGroup.substring(0,funcIdGroup.length()-1);
				for(int j=0;j<sysMenus.size();j++){
				if(funcIdGroup.indexOf(",") != -1){
					String[] func = funcIdGroup.split(",");
					int count = 0;
					for(int i=0;i<func.length;i++){
						SysFunc sysFunc = (SysFunc)dao.find(SysFunc.class, func[i]);
						
						String func1 = sysFunc.getMenuCodeGroup().substring(0,2);
						
						if(sysMenus.get(j).getMenuCode().equals(func1)){
							count++;
							 if(count != 0){
								 break;
							 }
						}
					}
					 if(count != 0){
						 sysMenus1.add(sysMenus.get(j));
					 }
				}else{
					String[] func = new String[1];
					if(StringUtils.isNotBlank(funcIdGroup)){
						func[0] = funcIdGroup;
						int count = 0;
						for(int i=0;i<func.length;i++){
							
							
							SysFunc sysFunc = (SysFunc)dao.find(SysFunc.class, func[i]);
							
							String func1 = sysFunc.getMenuCodeGroup().substring(0,2);
							if(sysMenus.get(j).getMenuCode().equals(func1)){
								count++;
							}
						}
						 if(count != 0){
							 sysMenus1.add(sysMenus.get(j));
						 }
					}
				}
				}
			
			}
		for(SysMenu sysMenu:sysMenus1){
			sysMenu.setSysFuncs(sysFuncList(sysMenu.getMenuId(),user));
		}
		return sysMenus1;
	}
	
	
	private String getPermissions(BaseUser user){
		String funcIdGroup = "";
		String roleId = user.getRoleId();
		if(StringUtils.isNotBlank(roleId)){
			BaseRole role = new BaseRole();
			role = (BaseRole)dao.find(BaseRole.class, roleId);
			if(StringUtils.isNotBlank(role.getFuncIdGroup())){
				
				funcIdGroup += role.getFuncIdGroup();
			}
		}
		String roleIdOther = user.getRoleIdOther();
		if(StringUtils.isNotBlank(roleIdOther)){
			roleIdOther = roleIdOther.substring(0,roleIdOther.length()-1);
			if(roleIdOther.indexOf(",") != -1){
				String roles[] = roleIdOther.split(",");
				for(int i=0;i<roles.length;i++){
					BaseRole role = new BaseRole();
					role = (BaseRole)dao.find(BaseRole.class,  roles[i]);
					
					if(StringUtils.isNotBlank(role.getFuncIdGroup())){
						funcIdGroup += role.getFuncIdGroup();
					}
				}
			}else{
				
				BaseRole role = new BaseRole();
				role = (BaseRole)dao.find(BaseRole.class,  roleIdOther);
				if(StringUtils.isNotBlank(role.getFuncIdGroup())){
					
					funcIdGroup += role.getFuncIdGroup();
				}
			}
		}
		return funcIdGroup;
	}
	
//	public List<SysFunc> sysFuncList(String menuId,BaseUser user) {
//		// TODO Auto-generated method stub
//		SysMenu sysMenu = (SysMenu) dao.find(SysMenu.class, menuId);
//		
//		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"__' order by menuCodeGroup";
//		 
//		List<SysFunc> sysFuncs = new ArrayList();
//		sysFuncs = dao.find(hql);
//	
//		List<SysFunc> sysFuncs1 = new ArrayList();
//		String funcIdGroup = getPermissions(user);
//		
//		if(StringUtils.isNotBlank(funcIdGroup)){
//			funcIdGroup = funcIdGroup.substring(0,funcIdGroup.length()-1);
//			for(int j=0;j<sysFuncs.size();j++){
//			if(funcIdGroup.indexOf(",") != -1){
//				String[] func = funcIdGroup.split(",");
//				int count = 0;
//				for(int i=0;i<func.length;i++){
//					SysFunc func1 = (SysFunc)dao.find(SysFunc.class, func[i]);
//					if(sysFuncs.get(j).getFuncId().equals(func1.getFuncId())){
//						count++;
//						 if(count != 0){
//							 break;
//						 }
//					}
//				}
//				 if(count != 0){
//					 sysFuncs1.add(sysFuncs.get(j));
//				 }
//			}else{
//				String[] func = new String[1];
//				if(StringUtils.isNotBlank(funcIdGroup)){
//					func[0] = funcIdGroup;
//					int count = 0;
//					for(int i=0;i<func.length;i++){
//						SysFunc func1 = (SysFunc)dao.find(SysFunc.class, func[i]);
//						if(sysFuncs.get(j).getFuncId().equals(func1.getFuncId())){
//							count++;
//						}
//					}
//					 if(count != 0){
//						 sysFuncs1.add(sysFuncs.get(j));
//					 }
//				}
//			}
//			}
//		}
//		return sysFuncs1;
//	}
	
	
	public List<SysFunc> sysFuncList(String menuId,BaseUser user) {
		// TODO Auto-generated method stub
		SysMenu sysMenu = (SysMenu) dao.find(SysMenu.class, menuId);
		
		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"__' order by menuCodeGroup";
		 
		List<SysFunc> sysFuncs = dao.find(hql);
	
		if(sysFuncs.size()>0){
			for(SysFunc sysFunc:sysFuncs){
				String hql2 = "from SysFunc where menuCodeGroup like '"+sysFunc.getMenuCodeGroup()+"__' order by menuCodeGroup";
				
				List<SysFunc> list2 = dao.find(hql2);
				sysFunc.setSysFuncs(list2);
			}
		}
		
		
		List<SysFunc> sysFuncs1 = new ArrayList();
		String funcIdGroup = getPermissions(user);
		
		if(StringUtils.isNotBlank(funcIdGroup)){
			funcIdGroup = funcIdGroup.substring(0,funcIdGroup.length()-1);
			for(int j=0;j<sysFuncs.size();j++){
			if(funcIdGroup.indexOf(",") != -1){
				String[] func = funcIdGroup.split(",");
				int count = 0;
				for(int i=0;i<func.length;i++){
					SysFunc func1 = (SysFunc)dao.find(SysFunc.class, func[i]);
					if(sysFuncs.get(j).getFuncId().equals(func1.getFuncId())){
						count++;
						 if(count != 0){
							 break;
						 }
					}
				}
				 if(count != 0){
					 sysFuncs1.add(sysFuncs.get(j));
				 }
			}else{
				String[] func = new String[1];
				if(StringUtils.isNotBlank(funcIdGroup)){
					func[0] = funcIdGroup;
					int count = 0;
					for(int i=0;i<func.length;i++){
						SysFunc func1 = (SysFunc)dao.find(SysFunc.class, func[i]);
						if(sysFuncs.get(j).getFuncId().equals(func1.getFuncId())){
							count++;
						}
					}
					 if(count != 0){
						 sysFuncs1.add(sysFuncs.get(j));
					 }
				}
			}
			}
		}
		
		return sysFuncs1;
	}
	@Override
	public List<SysMenu> findByCode(String menuCode) {
		// TODO Auto-generated method stub
		List<SysMenu> sysMenus = new ArrayList<SysMenu>();
		
		sysMenus = dao.find("from SysMenu where menuCode like '"+menuCode+"'");
		return sysMenus;
	}
	@Override
	public SysMenu findByMebuCode(String code) {
		// TODO Auto-generated method stub
		try{
			SysMenu sysMenu = new SysMenu();
			String hql = "from SysMenu where menuCode='"+code+"'";
			
			List list = dao.find(hql);
			
			if(list != null && list.size()>0){
				
				sysMenu= (SysMenu)list.get(0);
			}
			
			return sysMenu;
		}catch (Exception e) {
			return new SysMenu();
		}
	}
}
